<!-- 为了解决在安卓手机（或部分手机）中生成画布后里面的图片模糊问题，那么建议将该页面所有的 image 标签改为 img 标签，这样兼容性会更好些！ -->
<template>
	<view class="html2Canvas">
		<!-- 需要生成图片的模板 -->
		<view class="canvas-module" id="pagePoster">
			<view class="header">
				<view class="yuan" v-show="isShow" @click="handleTo"></view>
			</view>
			<view class="imgButton">
				<!-- 保存分享 -->
				<view class="generate-image" @click="canvasImage.generateImage">
					<view v-show="isShow" class="flex" >
						<view class="er text"></view>
					</view>
				</view>
				<view class="imgButton_b">
					<view class="imgButton_b_b">
						我的邀请码：{{model.code || ''}}
					</view>
				</view>
				<view class="qrcode" :style="{ backgroundImage: 'url(' + model.QRCode + ')' }"></view>
				<!-- <view class="c-white fs-30 mg-t-20">扫描二维码下载</view> -->
			</view>
			
		</view>
	</view>
</template>

<script>
	import {
		inviteFriends
	} from '@/api/my.js'
	export default {
		data() {
			return {
				model:{
					QRCode:'',
					code:''
				},
				QRCode: '',
				isShow: true,
				posterUrl: '', // 生成画布的图片
				userImage: '' // 本地头像图片
			};
		},
		onLoad() {
			this.getCode()
		},
		methods: {
			// 获取邀请码和二维码
			async getCode() {
				const res = await inviteFriends()
				this.model.QRCode = res.data.url
				this.model.code = res.data.code
			},
			// 返回上一页
			handleTo() {
				uni.navigateBack()
			},
			// base64转换存储到相册
			saveBase64Img(base64) {
				const bitmap = new plus.nativeObj.Bitmap('test');
				bitmap.loadBase64Data(base64, function() {
					const url = '_doc/' + new Date().getTime() + '.png'; // 使用时间戳命名文件
					bitmap.save(url, {
						overwrite: true
					}, i => {
						uni.saveImageToPhotosAlbum({
							filePath: url,
							success: function() {
								uni.showToast({
									title: '保存成功',
									icon: 'none'
								});
								bitmap.clear();
							},
							fail: function() {
								uni.showToast({
									title: '保存失败',
									icon: 'none'
								});
								bitmap.clear();
							}
						});
					}, e => {
						bitmap.clear();
					});
				}, e => {
					bitmap.clear();
				});
			},
			// 获取生成的base64 图片路径
			receiveRenderData(val) {
				this.posterUrl = val.replace(/[\r\n]/g, ''); // 去除base64位中的空格
				this.saveBase64Img(this.posterUrl)
			},

			// 显示loading
			_showLoading(str) {
				this.posterUrl = '';
				uni.showLoading({
					title: str
				});
			},
			// 隐藏保存分享文字
			_setShow() {
				this.isShow = !this.isShow
			},
			// 报错alert
			_errAlert(content) {
				uni.showModal({
					title: '提示',
					content: content
				});
			},
		}
	};
</script>

<script lang="renderjs" module="canvasImage">
	import html2canvas from 'html2canvas'
	export default {
		methods: {
			// 生成图片需要调用的方法
			generateImage(e, ownerFun) {
				ownerFun.callMethod('_setShow')
				ownerFun.callMethod('_showLoading', '正在生成图片') // 生成图片的 loading 提示
				setTimeout(() => {
					const dom = document.getElementById('pagePoster') // 需要生成图片内容的 dom 节点
					html2canvas(dom, {
						width: dom.clientWidth, //dom 原始宽度
						height: dom.clientHeight,
						scrollY: 0, // html2canvas默认绘制视图内的页面，需要把scrollY，scrollX设置为0
						scrollX: 0,
						useCORS: true, //支持跨域
						// scale: 2, // 设置生成图片的像素比例，默认是1，如果生成的图片模糊的话可以开启该配置项
					}).then((canvas) => {
						ownerFun.callMethod('_setShow')
						// 生成成功
						// html2canvas 生成成功的图片链接需要转成 base64位的url
						ownerFun.callMethod('receiveRenderData', canvas.toDataURL('image/png'))
					}).catch(err => {
						ownerFun.callMethod('_setShow')
						// 生成失败 弹出提示弹窗
						ownerFun.callMethod('_errAlert', `【生成图片失败，请重试】${err}`)
					})
				}, 300)
			}
		}
	}
</script>


<style lang="scss" scoped>
	.text {
		font-size: 40rpx;
		color: white;
		font-weight: 400;
	}
	.er{
		// border: white solid 1rpx;
		width: 500rpx;
		height: 300rpx;
		position: fixed;
		bottom: 0;
		left: 50%;
		transform: translate(-50%);
		// text-shadow: 3rpx 0 #00A0E9,
		// 	-3rpx 0 #00A0E9,
		// 	0 3rpx #00A0E9,
		// 	0 -3rpx #00A0E9,
		// 	3rpx 3rpx #00A0E9,
		// 	-3rpx -3rpx #00A0E9,
		// 	3rpx -3rpx #00A0E9,
		// 	-3rpx 3rpx #00A0E9;
	}
	.shImg {
		margin-left: 10rpx;
		width: 30rpx;
		height: 30rpx;
		background-size: 30rpx;
		background-position: center center;
		background-image: url('http://110.42.52.171:8802/profile/upload/img/2025/09/06/invite001_20250906094445A003.jpg');
		// background-image: url('http://192.168.1.99:8080/profile/upload/img/2025/07/24/1111_20250724134621A003.png');
	}

	.header {
		width: 100%;
		height: 250rpx;
		display: flex;
		align-items: center;
		justify-content: flex-start;
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.yuan {
		width: 80rpx;
		height: 80rpx;
		border-radius: 80rpx;
		background-size: 80rpx;
		background-position: center center;
		background-image: url('http://110.42.52.171:8802/profile/upload/img/2025/09/06/aa01_20250906100623A008.png');
	}

	.item-img {
		width: 100rpx;
		height: 100rpx;
	}

	.html2Canvas {
		.canvas-module {
			width: 100%;
			height: 100vh;
			// background-image: url('http://39.107.79.224:8080/profile/upload/hb/2025/08/25/invite_20250825112853A002.png');
			background-image: url('http://110.42.52.171:8890/profile/upload/2025/11/26/xjhb_20251126125953A003.png');
			background-size: cover;
			background-position: center;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			position: relative;
		}

		.generate-image {
			width: 100%;
			height: 150rpx;
			font-size: 34rpx;
			display: flex;
			align-items: flex-end;
			justify-content: center;
			color: #fff;
			position: absolute;
			bottom: 350rpx;
		}

		.sao {
			font-size: 30rpx;
			color: #FFFFFF;
			position: absolute;
			bottom: 30rpx;
			right: 110rpx;
		}

		.qrcode {
			width: 150rpx;
			height: 150rpx;
			// background-color: #3958F0;
			padding: 5rpx;
			box-sizing: border-box;
			position: absolute;
			bottom: 380rpx;
			left: 50%;
			transform: translate(-50%);
			// position: absolute;
			// right: 50rpx;
			// bottom: 75rpx;
			// background-image: url('');
			/* 默认背景 */
			background-size: cover;
			/* 背景填充 */
		}
		.imgButton_b_b{
			// background-color: #00A0E9;
			color: #FFFFFF;
			min-width: 400rpx;
			height: 65rpx;
			border-radius: 65rpx;
			padding: 0 30rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 36rpx;
		}
		.imgButton_b {	
			// background-color: #3958F0;
			width: 100%;
			height: 170rpx;
			color: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
			position: absolute;
			bottom: 250rpx;
			left: 50%;
			transform: translate(-50%);
			// padding: 0 80rpx;
			// box-sizing: border-box;
			// font-size: 34rpx;
		}

		.imgButton {
			width: 100%;
			height: 640rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			// justify-content: space-between;
			// position: relative;
			bottom: 85rpx;
			color: #FFFFFF;
			// background-color: red;
		}

	}
</style>